package com.hcbd.tdwt.ui.fuda.common.activity;

import android.annotation.SuppressLint;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.hcbd.tdwt.R;
import com.hcbd.tdwt.ui.fuda.common.adapter.WeatherAdapter;
import com.hcbd.tdwt.ui.fuda.common.vo.weather.WeatherInfo;
import com.hcbd.tdwt.ui.fuda.common.vo.weather.WeatherModeType;
import com.hcbd.tdwt.MainApp;
import com.hcbd.tdwt.base.BaseActivity;
import com.hcbd.tdwt.base.BeidouBoxParams;
import com.hcbd.tdwt.base.Constant;
import com.hcbd.tdwt.base.GlobalParams;
import com.hcbd.tdwt.util.DateUtils;
import com.raizlabs.android.dbflow.sql.language.SQLite;

import com.hcbd.tdwt.ui.xingyu.vo.Position;


import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;

import java.util.List;

import static com.hcbd.tdwt.base.GlobalParams.sendBeidouMsgSignalEnable;

@SuppressLint("NonConstantResourceId")
@EActivity(R.layout.activity_weather_helper)
public class WeatherHelperActivity extends BaseActivity {
    @ViewById(R.id.weather_listview)
    RecyclerView weatherListview;
    @ViewById
    TextView title;

    //数据列表
    private List<WeatherInfo> mList;
    //列表适配器
    private WeatherAdapter weatherAdapter;
    //获取天气类型
    private WeatherModeType weatherModeType;
    @AfterViews
    protected void init() {
        title.setText("天气助手");

    }


    @Click
    void headLeft(){
        finish();
    }
    /**
     * 获取天气预报
     *
     * @param weatherModeType
     */
    private void getWeather(WeatherModeType weatherModeType) {
        if (!BeidouBoxParams.isBoxConnectNormal) {
            MainApp.getInstance().showMsg(getString(R.string.please_connect_the_terminal_first));
            return;
        }
        int bdSignal = BeidouBoxParams.getBdSignal();
        if (bdSignal < sendBeidouMsgSignalEnable) {//信号是否良好
            toast(getString(R.string.please_move_the_terminal_location));
            return ;
        }
        if (BeidouBoxParams.sentWaitSec != 0) {
            toast("请等待" + BeidouBoxParams.sentWaitSec + "s后再发送");
            return;
        }
        Position position = new Position();
        position.setLongitude(GlobalParams.gpsLongitude);
        position.setLatitude(GlobalParams.gpsLatitude);
        position.setAltitude(GlobalParams.gpsAltitude);
        position.setTime(Long.parseLong(DateUtils.getTimeStamp()));
        WeatherInfo weatherInfo = new WeatherInfo();
        weatherInfo.setIoType(Constant.SEND_MESSAGE);
        weatherInfo.setJsonInfo(weatherModeType.getName());
        weatherInfo.setWeatherMode(WeatherModeType.Other.getValue());
        weatherInfo.setCreateTime(DateUtils.getSysDate());
        weatherInfo.insert();
        // 发送状态倒计时线程
        MainApp.getInstance().startNewSentWaitSecTimer();

    }
    //消息列表刷新
    public void initMsg(){
        mList= SQLite.select().from(WeatherInfo.class).queryList();
        if(weatherAdapter==null){
            weatherAdapter=new WeatherAdapter(this,mList);
            weatherAdapter.setHasStableIds(true);
        }else{
            weatherAdapter.upDateList(mList);
            return;
        }
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        weatherListview.setLayoutManager(linearLayoutManager);
        weatherListview.setAdapter(weatherAdapter);
        linearLayoutManager.setStackFromEnd(true);
    }
}
